package model

import (
	"shortURL/pool"
	"time"
)

type Record struct {
	Id       int       `gorm:"primary_key"`
	ShortUrl string    `gorm:"column:short_url"`
	Os       string    `gorm:"column:os"`
	Ip       string    `gorm:"column:ip"`
	CreateAt time.Time `gorm:"column:created_at"`
}

func NewRecord() *Record {
	return &Record{}
}

func (r *Record) TableName() string {
	return "record"
}

func (r *Record) Connect() string {
	return "default"
}

func (r *Record) Create() (id int, err error) {
	conn, err := pool.GetDbConn(r.Connect())
	if err != nil {
		return 0, err
	}
	defer pool.PutDbConn(r.Connect(), conn)

	if err := conn.Table(r.TableName()).Create(&r).Error; err != nil {
		return 0, err
	}

	return r.Id, nil
}
